import os

from PyQt5 import QtWidgets, QtGui
from PyQt5.QtCore import QEvent, Qt
from PyQt5.QtWidgets import QLabel, QUndoCommand

from config.config import Config


class CustomButton(QtWidgets.QPushButton):
    def __init__(self, *args, **kwargs):
        super(CustomButton, self).__init__(*args, **kwargs)
        self.setMouseTracking(True)  # 启用鼠标跟踪

    def event(self, event):
        if event.type() == QEvent.Enter:
            self.setStyleSheet(
                "border: 0px solid gray;"
                "border-radius: 10px;"
                "padding: 10px;"
                "background-color:rgb(95, 214, 255);")
        elif event.type() == QEvent.Leave:
            self.setStyleSheet(
                "border: 0px solid gray;"
                "border-radius: 10px;"
                "padding: 10px;"
                "background-color:rgb(189, 232, 255);")  # 恢复默认背景色
        return super(CustomButton, self).event(event)


class CustomToolButton(QtWidgets.QToolButton):
    def __init__(self, *args, **kwargs):
        super(CustomToolButton, self).__init__(*args, **kwargs)
        self.setMouseTracking(True)  # 启用鼠标跟踪

    def event(self, event):
        if event.type() == QEvent.Enter:
            self.setStyleSheet(
                "border: 0px solid gray;"
                "border-radius: 10px;"
                "padding: 5px;"
                "background-color:rgb(95, 214, 255);")
        elif event.type() == QEvent.Leave:
            self.setStyleSheet(
                "border: 0px solid gray;"
                "border-radius: 10px;"
                "padding: 5px;"
                "background-color:rgb(189, 232, 255);")  # 恢复默认背景色
        return super(CustomToolButton, self).event(event)


class DraggableLabel(QLabel):
    def __init__(self, main_window, *args, **kwargs):
        super(DraggableLabel, self).__init__(main_window, *args, **kwargs)
        self.setMouseTracking(True)
        self.main_window = main_window

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.window().drag_position = event.globalPos() - self.window().frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            if self.main_window.is_maximized:
                self.main_window.showNormal()
                self.main_window.is_maximized = False
            self.window().move(event.globalPos() - self.window().drag_position)
            event.accept()

    def mouseDoubleClickEvent(self, event):
        if event.button() == Qt.LeftButton:
            if self.main_window.is_maximized:
                self.main_window.showNormal()
                full_icon = QtGui.QIcon()
                full_icon.addPixmap(QtGui.QPixmap(os.path.join(Config.svg_path, 'full_screen.svg')), QtGui.QIcon.Normal,
                                    QtGui.QIcon.Off)
                self.main_window.toolButton_4.setIcon(full_icon)
                self.main_window.is_maximized = False
            else:
                self.main_window.showMaximized()
                full_icon = QtGui.QIcon()
                full_icon.addPixmap(QtGui.QPixmap(os.path.join(Config.svg_path, 'min_screen.svg')), QtGui.QIcon.Normal,
                                    QtGui.QIcon.Off)
                self.main_window.toolButton_4.setIcon(full_icon)
                self.main_window.is_maximized = True


# 撤回命令
class AddRemoveCommand(QUndoCommand):
    def __init__(self, scene, item, is_add):
        super().__init__()
        self.scene = scene
        self.item = item
        self.is_add = is_add

    def undo(self):
        if self.is_add:
            if isinstance(self.item, list):
                for x in self.item:
                    self.scene.removeItem(x)
            else:
                self.scene.removeItem(self.item)
        else:
            if isinstance(self.item, list):
                for x in self.item:
                    self.scene.addItem(x)
            else:
                self.scene.addItem(self.item)

    def redo(self):
        if self.is_add:
            if isinstance(self.item, list):
                for x in self.item:
                    self.scene.addItem(x)
            else:
                self.scene.addItem(self.item)
        else:
            if isinstance(self.item, list):
                for x in self.item:
                    self.scene.removeItem(x)
            else:
                self.scene.removeItem(self.item)
